<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2019/7/23
 * Time: 13:25
 */
namespace app\api\controller;


use app\api\ApiController;
use tools\Redis;

class Login extends ApiController
{
    function initialize()
    {
        $class = is_object($this) ? get_class($this) : $this;
        $pace = str_replace('\\', '/', $class);
        $classname = basename($pace) ;
//        实例化对应模型
        $this->newModel('\app\api\model\\'.$classname);
    }

    function wechat(){
        return 'wechat';
    }

    function ali(){
        return 'ali';
    }

    function phone(){
        return $this->request->data;
    }

    function password(){
        $data = $this->request->param();
        $classname = 'app\validate\Login.';
        if(!strstr($data['username'], '@')){
            $class='username';
        }else{
            $class='email';
        }
        $a = $this->validate( $data, $classname.$class);
        if($a!==true){
            $this->error($a);
        }
        $userid = $this->model->login($data,$class);
        if ($userid===false){
            $this->error($this->model->getError(),1011);
        }
        $redis = new Redis();
        $t = session_create_id();
        $redis->set('login_'.$userid,['token'=>$t],0);
        $this->success(['token'=>$t,'apid'=>$userid]);
    }

    function gettoken(){
        $redis = new Redis();
        $t = session_create_id();
        $redis->set($t,'nologin');
        $this->success(['token'=>$t]);
    }

    private function login(){

    }

    function test(){
        if($this->request->isGet()){
            return view('test');
        }else{
            dd($this->request);
        }
    }

}